#!/usr/bin/env bash

# REQUIRES: preload
# RUN: bash %s %T/assembly_sources
# RUN: cd %T/assembly_sources; %{intercept-build} --cdb preload.json make -C src
# RUN: cd %T/assembly_sources; %{cdb_diff} preload.json expected.json

set -o errexit
set -o nounset
set -o xtrace

# the test creates a subdirectory inside output dir.
#
# ${root_dir}
# ├── expected.json
# └── src
#    ├── Makefile
#    └── main.c

root_dir=$1
mkdir -p "${root_dir}/src"

cp "${test_input_dir}/main.c" "${root_dir}/src/main.c"

cat > ${root_dir}/src/Makefile << EOF
main: main.o
	\$(CC) $< -o \$@

main.s: main.c
	\$(CC) -S \$< -o \$@

main.o: main.s
	\$(CC) -c \$< -o \$@
EOF


cat > "${root_dir}/expected.json" << EOF
[
    {
        "arguments": [
            "cc",
            "-c",
            "-o",
            "main.o",
            "main.s"
        ],
        "directory": "${root_dir}/src",
        "file": "main.s"
    },
    {
        "arguments": [
            "cc",
            "-S",
            "-o",
            "main.s",
            "main.c"
        ],
        "directory": "${root_dir}/src",
        "file": "main.c"
    }
]
EOF
